#include <linux/uaccess.h>
#include <linux/fs.h>
#include <linux/init.h>
#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/kernel_stat.h>
#include <linux/platform_device.h>
#include <linux/kobject.h>
#include <linux/sysfs.h>
#include <linux/in.h>
#include <net/neighbour.h>
#include <net/arp.h>
#include <linux/if_arp.h>   /** type  */
#include <net/ip.h>
#include <net/netevent.h>
#include <net/route.h>
#include <net/ip_fib.h>
#include <linux/types.h>
#include <linux/netdevice.h>
#include <linux/netlink.h>
#include <linux/inetdevice.h>
#include <linux/list.h>
#include <linux/rcupdate.h>
#include <linux/export.h>
#include <net/tcp.h>
#include <net/fib_rules.h>

#include <uapi/linux/rtnetlink.h>

struct myfib_alias {
	struct hlist_node fa_list;
	int fa_type;
};

static HLIST_HEAD(leaf);

void for_each_entry_rcu(void) {
	struct myfib_alias *each;
	struct myfib_alias a1 = { .fa_type = 1, };
	struct myfib_alias a2 = { .fa_type = 2, };
	struct myfib_alias a3 = { .fa_type = 3, };
	struct myfib_alias a4 = { .fa_type = 4, };

//	struct myfib_alias a500 ={.fa_type =500,} ;

	hlist_add_head_rcu(&a1.fa_list, &leaf);
	hlist_add_head_rcu(&a2.fa_list, &leaf);
	hlist_add_head_rcu(&a3.fa_list, &leaf);
	hlist_add_head_rcu(&a4.fa_list, &leaf);

	hlist_for_each_entry_rcu(each, &leaf, fa_list)
	{
		pr_info("type: %d \n", each->fa_type);
	}
}

void add_before_rcu(void) {
	struct myfib_alias *each;
	struct myfib_alias a1 = { .fa_type = 10, };
	struct myfib_alias a2 = { .fa_type = 20, };
	struct myfib_alias a3 = { .fa_type = 30, };
	struct myfib_alias a4 = { .fa_type = 40, };

	struct myfib_alias a5 = { .fa_type = 25, };

	hlist_add_tail_rcu(&a1.fa_list, &leaf);
	hlist_add_tail_rcu(&a2.fa_list, &leaf);
	hlist_add_tail_rcu(&a3.fa_list, &leaf);
	hlist_add_tail_rcu(&a4.fa_list, &leaf);

	hlist_for_each_entry_rcu(each, &leaf, fa_list)
	{
		pr_info("type: %d \n", each->fa_type);
	}

	hlist_add_before_rcu(&a5.fa_list, &a3.fa_list);

	hlist_for_each_entry_rcu(each, &leaf, fa_list)
	{
		pr_info("type: %d \n", each->fa_type);
	}

}

void first_rcu(void) {
	struct hlist_node *first;
	struct myfib_alias *f;
	struct myfib_alias a1 = { .fa_type = 10, };
	struct myfib_alias a2 = { .fa_type = 20, };
	struct myfib_alias a3 = { .fa_type = 30, };
	struct myfib_alias a4 = { .fa_type = 40, };

	hlist_add_tail_rcu(&a1.fa_list, &leaf);
	hlist_add_tail_rcu(&a2.fa_list, &leaf);
	hlist_add_tail_rcu(&a3.fa_list, &leaf);
	hlist_add_tail_rcu(&a4.fa_list, &leaf);

	first = hlist_first_rcu(&leaf);
	f = container_of(first, struct myfib_alias, fa_list);
	pr_info("type: %d \n", f->fa_type);
}

void next_rcu(void) {
	struct hlist_node *next;
	struct myfib_alias *f;
	struct myfib_alias a1 = { .fa_type = 10, };
	struct myfib_alias a2 = { .fa_type = 20, };
	struct myfib_alias a3 = { .fa_type = 30, };
	struct myfib_alias a4 = { .fa_type = 40, };

	hlist_add_tail_rcu(&a1.fa_list, &leaf);
	hlist_add_tail_rcu(&a2.fa_list, &leaf);
	hlist_add_tail_rcu(&a3.fa_list, &leaf);
	hlist_add_tail_rcu(&a4.fa_list, &leaf);

	next = hlist_next_rcu(&a2.fa_list);

	f = container_of(next, struct myfib_alias, fa_list);
	pr_info("type: %d \n", f->fa_type);
}

void add_behind_rcu(void) {
	struct myfib_alias *each;
	struct myfib_alias a1 = { .fa_type = 10, };
	struct myfib_alias a2 = { .fa_type = 20, };
	struct myfib_alias a3 = { .fa_type = 30, };
	struct myfib_alias a4 = { .fa_type = 40, };

	struct myfib_alias a5 = { .fa_type = 25, };

	hlist_add_tail_rcu(&a1.fa_list, &leaf);
	hlist_add_tail_rcu(&a2.fa_list, &leaf);
	hlist_add_tail_rcu(&a3.fa_list, &leaf);
	hlist_add_tail_rcu(&a4.fa_list, &leaf);

	hlist_for_each_entry_rcu(each, &leaf, fa_list)
	{
		pr_info("type: %d \n", each->fa_type);
	}

	hlist_add_behind_rcu(&a5.fa_list, &a3.fa_list);

	hlist_for_each_entry_rcu(each, &leaf, fa_list)
	{
		pr_info("type: %d \n", each->fa_type);
	}

}

int myprobe(struct platform_device *pdev) {
//a();
//add_before_rcu();
//add_behind_rcu();
//	first_rcu();
	next_rcu();
	return 0;
}

int myremove(struct platform_device *pdev) {
	pr_info("myplatformdriver myremove \n");
	return 0;
}

struct of_device_id my_of_match_table =
		{ .compatible = "my_platform_device_003", };

struct platform_driver my_platform_driver = { .driver = { .of_match_table =
		&my_of_match_table, .name = "my-platform-driver", .owner =
THIS_MODULE, }, .probe = myprobe, .remove = myremove, };

module_platform_driver(my_platform_driver);

MODULE_LICENSE("GPL");
MODULE_AUTHOR("Andy");
MODULE_DESCRIPTION("andy one-key driver");
MODULE_ALIAS("one-key");
